package cn.iocoder.yudao.module.cms.feedback.controller.admin;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.cms.feedback.dal.dataobject.FeedbackDO;
import cn.iocoder.yudao.module.cms.feedback.service.FeedbackService;
import cn.iocoder.yudao.module.cms.feedback.vo.AdminFeedbackPageReqVO;
import cn.iocoder.yudao.module.cms.feedback.vo.AdminFeedbackRespVO;
import cn.iocoder.yudao.module.cms.feedback.vo.FeedbackReplyReqVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 意见反馈")
@RestController
@RequestMapping("/cms/feedback")
@Validated
public class FeedbackController {

    @Resource
    private FeedbackService feedbackService;

    @PostMapping("/reply")
    @Operation(summary = "回复意见反馈")
    @PreAuthorize("@ss.hasPermission('cms:feedback:update')")
    public CommonResult<Boolean> updateFeedbackInfo(@Valid @RequestBody FeedbackReplyReqVO updateReqVO) {
        feedbackService.replyFeedbackInfo(updateReqVO);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得意见反馈")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cms:feedback:query')")
    public CommonResult<AdminFeedbackRespVO> getFeedbackInfo(@RequestParam("id") Long id) {
        FeedbackDO feedback = feedbackService.getFeedbackInfo(id);
        return success(BeanUtils.toBean(feedback, AdminFeedbackRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得意见反馈分页")
    @PreAuthorize("@ss.hasPermission('cms:feedback:query')")
    public CommonResult<PageResult<AdminFeedbackRespVO>> getFeedbackPage(@Valid AdminFeedbackPageReqVO pageReqVO) {
        PageResult<FeedbackDO> pageResult = feedbackService.getFeedbackInfoPage(pageReqVO);
        List<AdminFeedbackRespVO> list = BeanUtils.toBean(pageResult.getList(), AdminFeedbackRespVO.class);
        return success(new PageResult<>(list, pageResult.getTotal()));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出意见反馈 Excel")
    @PreAuthorize("@ss.hasPermission('cms:feedback:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFeedbackExcel(@Valid AdminFeedbackPageReqVO pageReqVO,
                                    HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FeedbackDO> list = feedbackService.getFeedbackInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "意见反馈.xls", "数据", AdminFeedbackRespVO.class,
                BeanUtils.toBean(list, AdminFeedbackRespVO.class));
    }

}
